/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clipper.clipboard;

import clipper.tray.ClipTray;


/**
 *
 * @author mahadevan
 */
public class PollerThread implements Runnable {

    private String[] clipperTexts = new String[50];
    private ClipBored clipper;
    private int currentTextCount;
    private int lastTextCount;
    private boolean appExit = false;
    private ClipTray clipTray;

    public PollerThread() {
        //Single instance of ClipBored
        lastTextCount = -2;
        clipper = ClipBored.getClipBored();
        clipTray = new ClipTray();
    }

    public void pollForClipBoard() {

        clipperTexts = clipper.getClipboredTexts();
        currentTextCount = clipper.getTextCount();

        if (lastTextCount != currentTextCount) {

            lastTextCount = currentTextCount;

            if (clipperTexts == null) {
                return;
            }


                clipTray.addTextToMenu(clipperTexts[currentTextCount - 1],clipperTexts);
            
        }
    }

    public void run() {
        //poll for clipboard Change every 3 seconds.
        while (!appExit) {
            pollForClipBoard();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                System.out.println(ex.toString());
            }
        }
    }

    public boolean isFinished() {
        return appExit;
    }

    public void start() {
        Thread thread = new Thread(this);
        thread.start();

    }
}


